package cc.sends;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @ Description : SSTF Algorithm
 * @ Author : ChristosC
 * @ Date : 2020/12/16 21:25
 */
public class SSTF {
    List<Track> tracks;
    Track currTrack;
    float averageDistance;
    public void init(List<Track> tracks,Track currTrack){
        this.tracks = tracks;
        this.currTrack = currTrack;
        averageDistance = 0;
    }
    public void start(){
        int sumDistance = 0;
        int trackListSize = tracks.size();
        DecimalFormat df = new DecimalFormat("#0.00");
        while (tracks.size()>0){
            // 计算每个Track与currTrack的距离
            for(Track track : tracks){
                track.setDistanceToCurrTrack(Math.abs(currTrack.getTrackNum() - track.getTrackNum()));
            }
            // 取出distanceToCurrTrack最小的Track
            currTrack = getThePioneerTrack(tracks);
            System.out.println(currTrack.getTrackNum()+"\t"+currTrack.getDistanceToCurrTrack());
            tracks.remove(currTrack);
            sumDistance += currTrack.getDistanceToCurrTrack();
        }
        averageDistance = (float)sumDistance/trackListSize;
        System.out.println("平均寻道长度："+df.format(averageDistance));
    }
    private Track getThePioneerTrack(List<Track> tracks){
        Track result = tracks.get(0);
        int min = tracks.get(0).getDistanceToCurrTrack();
        for(Track track : tracks){
            if(track.getDistanceToCurrTrack()<min){
                min = track.getDistanceToCurrTrack();
                result = track;
            }
        }
        return result;
    }

}
